package com.opo5.logisticsmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.opo5.logisticsmanagement.Vo.CableVo;
import com.opo5.logisticsmanagement.Vo.PageVo;
import com.opo5.logisticsmanagement.Vo.StatisVo;
import com.opo5.logisticsmanagement.dao.MaterialDao;
import com.opo5.logisticsmanagement.domain.Locator;
import com.opo5.logisticsmanagement.domain.Material;
import com.opo5.logisticsmanagement.service.MaterialService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;


@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialDao, Material> implements MaterialService {

    @Autowired
    private MaterialDao materialDao;

    @Override
    public IPage<Material> getPage(int currentPage, int pageSize, Material material) {
        LambdaQueryWrapper<Material> lqw=new LambdaQueryWrapper<>();

        lqw.eq(Strings.isNotEmpty(material.getMaterialNumber()),Material::getMaterialNumber,material.getMaterialNumber());

        lqw.like(Strings.isNotEmpty(material.getMaterialName()),Material::getMaterialName,material.getMaterialName());

        lqw.like(Strings.isNotEmpty(material.getMaterialSupplier()),Material::getMaterialSupplier,material.getMaterialSupplier());

        IPage<Material> page=new Page<>(currentPage,pageSize);

        materialDao.selectPage(page,lqw);

        return page;
    }

    /**
     * 产生物料编号随机数
     * @return
     */
    public String produceNumber(){
        String str="";
        Random random=new Random();
        str+= (char)(random.nextInt((26))+65);
        for (int i = 0; i <15 ; i++) {
            str+=String.valueOf(random.nextInt(10));
        }
        return str;
    }

    /**
     * 根据库位id查询物料信息
     * @param id
     * @return
     */
    @Override
    public Locator getAll(Integer id) {
        return materialDao.getAll(id);
    }


    /**
     * 根据仓库名称，库位名称查询
     * @param currentPage
     * @param pageSize
     * @param material
     * @return
     */
    @Override
    public PageVo getPageByCondition(Integer currentPage, Integer pageSize, Material material) {
        if (currentPage<=0){
            currentPage=1;
        }

        Integer pageBegin=(currentPage-1)*pageSize;


        List<Material> materials = materialDao.getPageByCondition(pageBegin, pageSize, material);



        Integer total = materialDao.getTotal(material);
        Integer pages=(total-1)/pageSize+1;
        PageVo materialPage=new PageVo();
        materialPage.setRecords(materials);
        materialPage.setTotal(total);
        materialPage.setPages(pages);
        materialPage.setCurrent(currentPage);
        materialPage.setSize(pageSize);
        return materialPage;
    }

    @Override
    public Page<StatisVo> getPageVo(Page<CableVo> iPage) {
        return null;
    }

//    @Override
//    public Page<StatisVo> getPageVo(Page<StatisVo> iPage) {
//        return materialDao.materials(iPage);
//    }
// public Page<NewVo> getPageVo(Page<NewVo> iPage) {
//        return newDao.newpro(iPage);
//    }

}
